import java.sql.*;

public class Main {
    public static void main(String[] args){
            // 1、加载驱动
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");

                // 2、获取连接 (url username password)
                String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
                Connection connection = DriverManager.getConnection(url, "root", "coup");
//            if (connection != null) {
//                System.out.println("连接成功");
//            }
                // 3、写sql
                String sql = "select * from user2";

                // 4、预编译sql
                PreparedStatement preparedStatement = connection.prepareStatement(sql);

                // 5、有参数设置参数

                // 6、执行sql
                ResultSet resultSet = preparedStatement.executeQuery();

                // 7、遍历结果集
                while (resultSet.next()){
                    int id = resultSet.getInt(1);
                    String name = resultSet.getString(2);
                    String pass = resultSet.getString(3);
                    System.out.println(id+"-----"+name+"------"+pass);
                }

                // 8、关闭资源
                resultSet.close();
                preparedStatement.close();
                connection.close();


            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
    }
}